package com.i2works.smartluolong.web.controller.government;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.entity.news.NewsDO;
import com.i2works.smartluolong.web.entity.news.NewsQuery;
import com.i2works.smartluolong.web.service.news.INewsService;

@Controller
@RequestMapping("/govenment")
public class GovenmentController {
	
	@Autowired
	private final INewsService newsService;
	
	public GovenmentController(INewsService newsService) {
		this.newsService=newsService;
	}
	

	 
    @GetMapping("/index")
    public String index(Model model) {
    	model.addAttribute("newsType", 2);
        return "govenment/index";
    }
    
    @GetMapping("/{newsType}")
    public String type(@PathVariable("newsType") Integer newsType,Model model) {
    	model.addAttribute("newsType", newsType);
        return "govenment/index";
    }
    
    
    
    @GetMapping("/list")
    @ResponseBody
    public Result list(NewsQuery newsQuery) {
	    newsQuery.setStatus(1);
	    newsQuery.setLimit(10L);
	    try {
	    Integer count=newsService.queryCount(newsQuery);
		List<NewsDO>  list=newsService.queryList(newsQuery);
		return R.success(list, count);
		} catch (ServiceException e) {
			e.printStackTrace();
			return R.error();
		}

    }
    
    @GetMapping("/view/{newsType}/{id}/{title}")
    public String view(@PathVariable("newsType") Integer newsType,@PathVariable("id") Integer id,@PathVariable("title") String title,Model model) {
    	try {
    		title=URLDecoder.decode(title, "utf-8");
        model.addAttribute("id", id);
    	model.addAttribute("title", title);
    	model.addAttribute("newsType", newsType);
    	} catch (UnsupportedEncodingException e) {
    		e.printStackTrace();
    	}
    	  return "govenment/view";
    }


}
